// check_bank_transactions.js
// 检查银行流水表中的数据

const SequelizeLib = require('sequelize');

// 数据库配置（使用项目中的配置）
const sequelize = new SequelizeLib({
  dialect: 'mysql',
  host: '127.0.0.1',
  port: 3306,
  username: 'root',
  password: '888mei525',
  database: 'lingxi_system',
  logging: false
});

// 银行流水模型定义
const BankTransaction = sequelize.define('bankTransaction', {
  id: {
    type: SequelizeLib.DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  organizationId: {
    type: SequelizeLib.DataTypes.INTEGER,
    allowNull: false,
    field: 'organization_id'
  },
  transactionId: {
    type: SequelizeLib.DataTypes.STRING(200),
    allowNull: false,
    unique: true,
    field: 'transaction_id'
  },
  payerName: {
    type: SequelizeLib.DataTypes.STRING(200),
    allowNull: false,
    field: 'payer_name'
  },
  payerAccount: {
    type: SequelizeLib.DataTypes.STRING(100),
    field: 'payer_account'
  },
  amount: {
    type: SequelizeLib.DataTypes.DECIMAL(12, 2),
    allowNull: false
  },
  transactionDate: {
    type: SequelizeLib.DataTypes.DATE,
    allowNull: false,
    field: 'transaction_date'
  },
  transactionTime: {
    type: SequelizeLib.DataTypes.DATE,
    allowNull: false,
    field: 'transaction_time'
  },
  balance: {
    type: SequelizeLib.DataTypes.DECIMAL(12, 2)
  },
  remark: {
    type: SequelizeLib.DataTypes.TEXT
  },
  dataSource: {
    type: SequelizeLib.DataTypes.ENUM('file_import', 'plugin_capture'),
    defaultValue: 'file_import',
    field: 'data_source'
  },
  importBatch: {
    type: SequelizeLib.DataTypes.STRING(100),
    field: 'import_batch'
  },
  isFiltered: {
    type: SequelizeLib.DataTypes.BOOLEAN,
    defaultValue: false,
    field: 'is_filtered'
  },
  filterReason: {
    type: SequelizeLib.DataTypes.STRING(100),
    field: 'filter_reason'
  },
  createdAt: {
    type: SequelizeLib.DataTypes.DATE,
    field: 'created_at'
  },
  bankName: {
    type: SequelizeLib.DataTypes.STRING(200),
    field: 'bank_name'
  },
  currency: {
    type: SequelizeLib.DataTypes.STRING(50)
  },
  channel: {
    type: SequelizeLib.DataTypes.STRING(50)
  },
  fee: {
    type: SequelizeLib.DataTypes.DECIMAL(12, 2),
    defaultValue: 0
  },
  accountName: {
    type: SequelizeLib.DataTypes.STRING(200),
    field: 'account_name'
  },
  accountNumber: {
    type: SequelizeLib.DataTypes.STRING(100),
    field: 'account_number'
  }
}, {
  tableName: 'bank_transactions',
  timestamps: false
});

// 检查银行流水数据
async function checkBankTransactions() {
  try {
    console.log('🔍 连接数据库...');
    await sequelize.authenticate();
    console.log('✅ 数据库连接成功!');
    
    console.log('🔍 查询银行流水数据...');
    const transactions = await BankTransaction.findAll({
      order: [['createdAt', 'DESC']],
      limit: 10
    });
    
    console.log(`📊 找到 ${transactions.length} 条银行流水记录:`);
    
    transactions.forEach((transaction, index) => {
      console.log(`\n--- 记录 ${index + 1} ---`);
      console.log(`  流水号: ${transaction.transactionId}`);
      console.log(`  对方户名: ${transaction.payerName}`);
      console.log(`  交易金额: ${transaction.amount}`);
      console.log(`  交易日期: ${transaction.transactionDate}`);
      console.log(`  银行名称: ${transaction.bankName}`);
      console.log(`  数据来源: ${transaction.dataSource}`);
      console.log(`  导入批次: ${transaction.importBatch}`);
    });
    
    // 检查是否有来自插件捕获的数据
    const pluginTransactions = await BankTransaction.count({
      where: {
        dataSource: 'plugin_capture'
      }
    });
    
    console.log(`\n📈 来自插件捕获的银行流水记录数: ${pluginTransactions}`);
    
    return transactions;
  } catch (error) {
    console.error('❌ 检查银行流水数据失败:', error.message);
    throw error;
  } finally {
    await sequelize.close();
  }
}

// 主函数
async function main() {
  try {
    console.log('🚀 开始检查银行流水数据...');
    await checkBankTransactions();
    console.log('✅ 检查完成!');
  } catch (error) {
    console.error('❌ 检查失败:', error.message);
    process.exit(1);
  }
}

// 执行主函数
if (require.main === module) {
  main();
}

module.exports = {
  checkBankTransactions
};